package com.eascs.ticket.project.controller;

import java.util.List;

import javax.inject.Inject;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.eascs.common.util.ObjectUtil;
import com.eascs.common.util.UrlUtil;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.page.SelectDataOption;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.PaginationRestResult;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.ButtonHelper;
import com.eascs.ticket.common.entity.EntityUtil;
import com.eascs.ticket.project.entity.Project;
import com.eascs.ticket.project.entity.ProjectUpdate;
import com.eascs.ticket.project.form.SaveProjectForm;
import com.eascs.ticket.project.service.ProjectService;
import com.eascs.web.url.UrlHelper;

@RestController
public class ProjectRestController extends BaseRestController {
	@Inject
	private ProjectService projectService;

	@RequestMapping(value = "/project/save")
	public BaseRestResult<String> save(SaveProjectForm saveProjectForm) {
		if (SaveProjectForm.EDIT_TYPE_UPDATE.equals(saveProjectForm.getEditType())) {
			ProjectUpdate projectUpdate = new ProjectUpdate();
			ObjectUtil.copySimpleProperties(saveProjectForm, projectUpdate);
			EntityUtil.initUpdateEntity(projectUpdate);
			projectService.updateProject(projectUpdate);
		} else if (SaveProjectForm.EDIT_TYPE_CREATE.equals(saveProjectForm.getEditType())) {
			Project project = new Project();
			ObjectUtil.copySimpleProperties(saveProjectForm, project);
			EntityUtil.initInsertEntity(project);
			// id to lower
			project.setId(project.getId().toLowerCase().trim());
			projectService.insertProject(project);
		}
		return BaseRestResult.info("操作成功").andRedirect(UrlUtil.appendUrl(UrlHelper.build("/project/view"), "projectId", saveProjectForm.getId()));
	}

	@RequestMapping(value = "/project/get-valid-projects")
	public PaginationRestResult getValidProjects() {
		List<Object[]> projects = projectService.getValidProjectViewList();
		PaginationInfo paginationInfo = new PaginationInfo(projects, new String[] { "项目" });
		paginationInfo.setBuildPagingNavigation(false);
		paginationInfo.setMoreButtons(new Button[] { ButtonHelper.createOpenButton("查看项目", "/project/view?projectId="), ButtonHelper.createOpenButton("增加工单", "/ticket/add?projectId="), ButtonHelper.createOpenButton("查看所有工单", "/ticket/view/search?projectIds="),
				ButtonHelper.createOpenButton("查看没关闭的工单", "/ticket/view/search?statusNotIns=CLOSE&projectIds="), ButtonHelper.createOpenButton("查看没关闭的版本", "/ticket/view/search?statusNotIns=CLOSE&ticketTypeCodes=version&projectIds=") });
		return PaginationRestResult.buildPaginationRestResult(paginationInfo);
	}

	@RequestMapping(value = "/project/get-all-projects")
	public PaginationRestResult getAllProjects() {
		List<Object[]> projects = projectService.getAllProjectViewList();
		PaginationInfo paginationInfo = new PaginationInfo(projects, new String[] { "项目", "状态" });
		paginationInfo.setSelectDataOptions(new SelectDataOption[] { null, new SelectDataOption("status") });
		paginationInfo.setBuildPagingNavigation(false);
		paginationInfo.setMoreButtons(new Button[] { ButtonHelper.createOpenButton("查看项目", "/project/view?projectId="), ButtonHelper.createOpenButton("增加工单", "/ticket/add?projectId="), ButtonHelper.createOpenButton("查看所有工单", "/ticket/view/search?projectIds="),
				ButtonHelper.createOpenButton("查看没关闭的工单", "/ticket/view/search?statusNotIns=CLOSE&projectIds="), ButtonHelper.createOpenButton("查看没关闭的版本", "/ticket/view/search?statusNotIns=CLOSE&ticketTypeCodes=version&projectIds=") });
		return PaginationRestResult.buildPaginationRestResult(paginationInfo);
	}

}
